﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    private string uploadDir; //uploadDir变量存放文件保存路径

    protected void Page_Load(object sender, EventArgs e)
    {
        //默认将文件保存到网站根文件夹下的image子文件夹中
        uploadDir = Path.Combine(Request.PhysicalApplicationPath, "image");

        //页面首次加载且商品ID不为空
        if (!IsPostBack & Request.QueryString["GoodsID"] != null)
        {

            MyDataDataContext db = new MyDataDataContext();

            //获取要修改的出售信息的记录
            var goods = (from r in db.SaleOfGoods
                         where r.GoodsID == Request.QueryString["GoodsID"]
                         select r).First();

            //显示出售信息
            //商品图片路径不为空，显示头像
            if (goods.GoodsPicture != null)
            {
                Image1.ImageUrl = goods.GoodsPicture;
            }
            //商品名称
            GoodsName.Text = goods.GoodsName;
            //商品描述
            GoodsDescription.Text = goods.GoodsDescription;
            //商品价格
            Price.Text = Convert.ToString(goods.Price);
            //商品分类
            Type.SelectedValue = goods.Type;

            //获取商品分类信息
            var results = from r in db.GoodsType
                          select new
                          {
                              r.GoodsTypeName
                          };//映射需要显示的字段

            //设置DropDownList控件的数据源
            Type.DataSource = results;
            //调用DataBind()方法显示数据
            Type.DataBind();

        }
    }

    //更换商品图片
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (fupImage.PostedFile.FileName == "")//无文件上传
        {
            lblMsg.Text = "无文件上传！";
        }
        else //有文件上传
        {
            if (fupImage.PostedFile.ContentLength > 104857600)//文件大小超过200KB
            {
                lblMsg.Text = "文件大小不能超过100MKB！";
            }
            else //文件大小未超过200KB
            {
                string extension = Path.GetExtension(fupImage.PostedFile.FileName);
                switch (extension.ToLower()) //判断文件类型
                {
                    case ".png":
                    case ".bmp":
                    case ".jpeg":
                    case ".jpg":
                        break;
                    default:         //文件扩展名不是png、bmp、jpg、jpeg
                        lblMsg.Text = "文件扩展名必须是png、bmp、jpg或jpeg！";
                        return;
                }

                //获取上传文件的文件名
                string fileName = Path.GetFileName(fupImage.PostedFile.FileName);
                //上传的文件将以原文件名保存到网站根目录文件夹下的image子文件夹中
                string fullPath = Path.Combine(uploadDir, fileName);

                try
                {
                    fupImage.PostedFile.SaveAs(fullPath); //上传文件
                    Image1.ImageUrl = "image/" + fileName;//显示商品的新图片

                    MyDataDataContext db = new MyDataDataContext();
                    //获取要修改的出售信息的记录
                    SaleOfGoods goods = (from r in db.SaleOfGoods
                                         where r.GoodsID == Request.QueryString["GoodsID"]
                                         select r).First();

                    //图片路径保存至数据库
                    goods.GoodsPicture = "image/" + fileName;
                    //提交
                    db.SubmitChanges();
                    lblMsg.Text = "图片更换成功！";
                }
                catch (Exception ee)
                {
                    lblMsg.Text = ee.Message; //上传文件失败，显示出错信息
                }
            }
        }
    }

    //删除商品
    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        //不需要检查控件验证是否通过
        //获取要删除的对象
        MyDataDataContext db = new MyDataDataContext();
        SaleOfGoods DeleteObj = (from r in db.SaleOfGoods
                                 where r.GoodsID == Request.QueryString["GoodsID"]
                                 select r).FirstOrDefault();

        //删除
        db.SaleOfGoods.DeleteOnSubmit(DeleteObj);
        db.SubmitChanges();

        //返回信息列表页面
        Response.Redirect("ChangeInformation.aspx");
    }

    //修改商品信息
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        //控件全部通过验证
        if (Page.IsValid)
        {
            MyDataDataContext db = new MyDataDataContext();
            //获取要修改的商品记录
            SaleOfGoods goods1 = (from r in db.SaleOfGoods
                                  where r.GoodsID == Request.QueryString["GoodsID"]
                                  select r).FirstOrDefault();


            //获取用户修改的内容并提交到数据库
            //商品名称
            goods1.GoodsName = GoodsName.Text.Trim();
            //商品描述
            goods1.GoodsDescription = GoodsDescription.Text.Trim();
            //商品价格
            goods1.Price = double.Parse(Price.Text.Trim());
            //商品类型
            goods1.Type = Type.SelectedValue;
            //根据商品类型名找出对应的商品类型ID
            GoodsType typeID = (from r in db.GoodsType
                                where r.GoodsTypeName == Type.SelectedValue
                                select r).First();
            //商品类型ID
            goods1.GoodsTypeID = typeID.GoodsTypeID;
            //修改时间,格式为 年/月/日 时:分:秒
            goods1.ReleaseTime = DateTime.Now.ToString();

            //提交
            db.SubmitChanges();

            //返回信息列表页面
            Response.Redirect("ChangeInformation.aspx");
        }
    }

}